load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library", "rust_test")

package(default_visibility = ["//visibility:public"])

rust_library(
    name = "pocket-ic",
    srcs = glob(["src/**/*.rs"]),
    compile_data = ["README.md"],
    proc_macro_deps = [
        "@crate_index//:strum_macros",
    ],
    version = "11.0.0",
    deps = [
        # Keep sorted.
        "@crate_index//:backoff",
        "@crate_index//:base64",
        "@crate_index//:candid",
        "@crate_index//:flate2",
        "@crate_index//:hex",
        "@crate_index//:ic-certification",
        "@crate_index//:ic-management-canister-types",
        "@crate_index//:ic-transport-types",
        "@crate_index//:reqwest",
        "@crate_index//:schemars",
        "@crate_index//:semver",
        "@crate_index//:serde",
        "@crate_index//:serde_bytes",
        "@crate_index//:serde_cbor",
        "@crate_index//:serde_json",
        "@crate_index//:sha2",
        "@crate_index//:slog",
        "@crate_index//:strum",
        "@crate_index//:tempfile",
        "@crate_index//:thiserror",
        "@crate_index//:tokio",
        "@crate_index//:tracing",
        "@crate_index//:tracing-appender",
        "@crate_index//:tracing-subscriber",
    ],
)

rust_doc_test(
    name = "pocket-ic-doc-test",
    crate = ":pocket-ic",
)

rust_test(
    name = "pocket-ic-test",
    compile_data = ["README.md"],
    crate = ":pocket-ic",
    deps = ["//packages/ic-error-types"],
)

rust_test(
    name = "tests",
    # the test sometimes times out on CI with default timeout
    # of "moderate" (5 minutes) - 2025-07-03
    timeout = "long",
    srcs = [
        "tests/common.rs",
        "tests/tests.rs",
    ],
    data = [
        "//packages/pocket-ic/test_canister:test_canister.wasm.gz",
        "//rs/pocket_ic_server:pocket-ic-server",
        "@mozilla_root_ca_store//file",
    ],
    env = {
        "RUST_TEST_THREADS": "4",
        "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
        "SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
        "TEST_WASM": "$(rootpath //packages/pocket-ic/test_canister:test_canister.wasm.gz)",
    },
    flaky = True,
    tags = [
        "cpu:16",
        # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
        # Failed to bind PocketIC server to address 127.0.0.1:0
        "requires-network",
        "test_all_platforms",
    ],
    deps = [
        # Keep sorted.
        ":pocket-ic",
        "//rs/registry/client",
        "//rs/registry/helpers",
        "//rs/registry/proto_data_provider",
        "//rs/types/base_types",
        "@crate_index//:bitcoin-0.28.2",
        "@crate_index//:candid",
        "@crate_index//:ed25519-dalek",
        "@crate_index//:flate2",
        "@crate_index//:hex",
        "@crate_index//:ic-certification",
        "@crate_index//:ic-management-canister-types",
        "@crate_index//:ic-transport-types",
        "@crate_index//:ic-vetkeys",
        "@crate_index//:k256",
        "@crate_index//:reqwest",
        "@crate_index//:serde",
        "@crate_index//:serde_cbor",
        "@crate_index//:sha2",
        "@crate_index//:tempfile",
        "@crate_index//:tokio",
        "@crate_index//:wat",
    ],
)

rust_test(
    name = "unix",
    # the test sometimes times out on CI with default timeout
    # of "moderate" (5 minutes) - 2025-07-03
    timeout = "long",
    srcs = [
        "tests/common.rs",
        "tests/unix.rs",
    ],
    data = [
        "//packages/pocket-ic/test_canister:test_canister.wasm.gz",
        "//rs/pocket_ic_server:pocket-ic-server",
        "@mozilla_root_ca_store//file",
    ],
    env = {
        "RUST_TEST_THREADS": "4",
        "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
        "SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
        "TEST_WASM": "$(rootpath //packages/pocket-ic/test_canister:test_canister.wasm.gz)",
    },
    flaky = True,
    tags = [
        "cpu:16",
        # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
        # Failed to bind PocketIC server to address 127.0.0.1:0
        "requires-network",
        "test_all_platforms",
    ],
    deps = [
        # Keep sorted.
        ":pocket-ic",
        "//rs/registry/canister",
        "//rs/registry/client",
        "//rs/registry/helpers",
        "//rs/registry/proto_data_provider",
        "//rs/registry/transport",
        "//rs/types/base_types",
        "@crate_index//:candid",
        "@crate_index//:hex",
        "@crate_index//:ic-management-canister-types",
        "@crate_index//:prost",
        "@crate_index//:reqwest",
        "@crate_index//:serde",
        "@crate_index//:tempfile",
        "@crate_index//:tokio",
    ],
)

[
    rust_test(
        name = "icp_features" + name_suffix,
        # the test sometimes times out on CI with default timeout
        # of "moderate" (5 minutes) - 2025-07-03
        timeout = "long",
        srcs = [
            "tests/common.rs",
            "tests/icp_features.rs",
        ],
        crate_root = "tests/icp_features.rs",
        data = [
            "//packages/pocket-ic/test_canister:test_canister.wasm.gz",
            "//rs/pocket_ic_server:pocket-ic-server" + name_suffix,
        ],
        env = {
            "RUST_TEST_THREADS": "2",
            "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server" + name_suffix + ")",
            "TEST_WASM": "$(rootpath //packages/pocket-ic/test_canister:test_canister.wasm.gz)",
        },
        tags = [
            "cpu:16",
            # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
            # Failed to bind PocketIC server to address 127.0.0.1:0
            "requires-network",
            "long_test",  # The P90 of this test is over 8m for the week starting on 2025-08-20
        ] + (["test_all_platforms"] if name_suffix == "" else []),  # do not build NNS canisters on all platforms
        deps = [
            # Keep sorted.
            ":pocket-ic",
            "//packages/icrc-ledger-types",
            "//rs/migration_canister:ic_migration_canister",
            "//rs/nns/cmc",
            "//rs/nns/governance/api",
            "//rs/nns/sns-wasm",
            "//rs/registry/canister",
            "//rs/types/base_types",
            "@crate_index//:candid",
            "@crate_index//:flate2",
            "@crate_index//:reqwest",
            "@crate_index//:serde",
            "@crate_index//:sha2",
            "@crate_index//:tempfile",
            "@crate_index//:tokio",
        ],
    )
    for name_suffix in [
        "-head-nns",
        "",
    ]
]

rust_test(
    name = "restart",
    size = "medium",
    srcs = ["tests/restart.rs"],
    data = [
        "//rs/pocket_ic_server:pocket-ic-server",
        "@mozilla_root_ca_store//file",
    ],
    env = {
        "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
        "SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
    },
    tags = [
        # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
        # Failed to bind PocketIC server to address 127.0.0.1:0
        "requires-network",
        "test_all_platforms",
    ],
    deps = [
        ":pocket-ic",
        "@crate_index//:reqwest",
    ],
)

rust_test(
    name = "slow",
    size = "medium",
    srcs = ["tests/slow.rs"],
    data = [
        "//packages/pocket-ic/test_canister:test_canister.wasm.gz",
        "//rs/pocket_ic_server:pocket-ic-server",
        "@mozilla_root_ca_store//file",
    ],
    env = {
        "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
        "SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
        "TEST_WASM": "$(rootpath //packages/pocket-ic/test_canister:test_canister.wasm.gz)",
    },
    tags = [
        "cpu:16",
        # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
        # Failed to bind PocketIC server to address 127.0.0.1:0
        "requires-network",
        "test_all_platforms",
    ],
    deps = [
        ":pocket-ic",
        "@crate_index//:candid",
    ],
)

rust_test(
    name = "canister_snapshots",
    size = "medium",
    srcs = ["tests/canister_snapshots.rs"],
    data = [
        "//:dfx",
        "//packages/pocket-ic/test_canister:test_canister.wasm.gz",
        "//rs/pocket_ic_server:pocket-ic-server",
        "@mozilla_root_ca_store//file",
    ],
    env = {
        "DFX": "$(rootpath //:dfx)",
        "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
        "SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
        "TEST_WASM": "$(rootpath //packages/pocket-ic/test_canister:test_canister.wasm.gz)",
    },
    tags = [
        "cpu:16",
        # TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
        # Failed to bind PocketIC server to address 127.0.0.1:0
        "requires-network",
        "test_all_platforms",
    ],
    deps = [
        ":pocket-ic",
        "//rs/types/management_canister_types",
        "@crate_index//:candid",
        "@crate_index//:hex",
        "@crate_index//:serde_json",
        "@crate_index//:tempfile",
    ],
)
